<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="遇到一个比较特殊的页面">
<meta property="og:type" content="article">
<meta property="og:title" content="tabBarItemClick">
<meta property="og:url" content="http://yoursite.com/2017/10/19/tabBarItemClick/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="遇到一个比较特殊的页面">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwdabpicyuj30bi0jggrg.jpg">
<meta property="og:updated_time" content="2019-07-19T05:40:06.063Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="tabBarItemClick">
<meta name="twitter:description" content="遇到一个比较特殊的页面">
<meta name="twitter:image" content="https://ws3.sinaimg.cn/large/006tNbRwly1fwdabpicyuj30bi0jggrg.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/10/19/tabBarItemClick/"/>





  <title>tabBarItemClick | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/19/tabBarItemClick/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">tabBarItemClick</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-10-19T09:24:38+08:00">
                2017-10-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/UI/" itemprop="url" rel="index">
                    <span itemprop="name">UI</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/10/19/tabBarItemClick/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2017/10/19/tabBarItemClick/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="遇到一个比较特殊的页面"><a href="#遇到一个比较特殊的页面" class="headerlink" title="遇到一个比较特殊的页面"></a>遇到一个比较特殊的页面<a id="more"></a></h4><p><img src="https://ws3.sinaimg.cn/large/006tNbRwly1fwdabpicyuj30bi0jggrg.jpg" alt=""></p>
<h4 id="利用UITabBarControllerDelegate"><a href="#利用UITabBarControllerDelegate" class="headerlink" title="利用UITabBarControllerDelegate"></a>利用UITabBarControllerDelegate</h4><blockquote>
<p><strong>-(BOOL)tabBarController:(UITabBarController <em>)tabBarController shouldSelectViewController:(UIViewController </em>)viewController</strong></p>
</blockquote>
<h4 id="首先创建RootController"><a href="#首先创建RootController" class="headerlink" title="首先创建RootController"></a>首先创建RootController</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions &#123;</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];</span><br><span class="line">    self.window.backgroundColor = [UIColor whiteColor];</span><br><span class="line">    RViewController *rvc = [[RViewController alloc]init];</span><br><span class="line">    WViewController *wvc = [[WViewController alloc]init];</span><br><span class="line">    TViewController *tvc = [[TViewController alloc]init];</span><br><span class="line">    </span><br><span class="line">    RootNavViewController *rnav = [[RootNavViewController alloc]initWithRootViewController:rvc];</span><br><span class="line">    RootNavViewController *wnav = [[RootNavViewController alloc]initWithRootViewController:wvc];</span><br><span class="line">    RootNavViewController *tnav = [[RootNavViewController alloc]initWithRootViewController:tvc];</span><br><span class="line">    </span><br><span class="line">    RootTabBarViewController *tabbarVC = [[RootTabBarViewController alloc]init];</span><br><span class="line">    [tabbarVC addChildViewController:rnav];</span><br><span class="line">    [tabbarVC addChildViewController:wnav];</span><br><span class="line">    [tabbarVC addChildViewController:tnav];</span><br><span class="line">    </span><br><span class="line">    self.window.rootViewController = tabbarVC;</span><br><span class="line">    [self.window makeKeyAndVisible];</span><br><span class="line">    </span><br><span class="line">    rnav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@&quot;首页&quot; image:[UIImage imageNamed:@&quot;jihuiN&quot;] selectedImage:[UIImage imageNamed:@&quot;jihui&quot;]];</span><br><span class="line">    wnav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@&quot;菜单&quot; image:[UIImage imageNamed:@&quot;linliN&quot;] selectedImage:[UIImage imageNamed:@&quot;linli&quot;]];</span><br><span class="line">    tnav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@&quot;我的&quot; image:[UIImage imageNamed:@&quot;renmaiN&quot;] selectedImage:[UIImage imageNamed:@&quot;renmai&quot;]];</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    return YES;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="创建TabbarItemPopoverView-PopoverView"><a href="#创建TabbarItemPopoverView-PopoverView" class="headerlink" title="创建TabbarItemPopoverView(PopoverView)"></a>创建TabbarItemPopoverView(PopoverView)</h4><h5 id="接口页"><a href="#接口页" class="headerlink" title="接口页"></a>接口页</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">#import &lt;UIKit/UIKit.h&gt;</span><br><span class="line"></span><br><span class="line">@protocol TabbarItemPopoverViewDelegate &lt;NSObject&gt;</span><br><span class="line">// 告诉外界点击了的按钮</span><br><span class="line">- (void)selecteItemIndex:(NSInteger)itemIndex buttonIndex:(NSInteger)btnIndex;</span><br><span class="line"></span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@interface TabbarItemPopoverView : UIView</span><br><span class="line"></span><br><span class="line">/**Delegate*/</span><br><span class="line">@property(weak,nonatomic) id &lt;TabbarItemPopoverViewDelegate&gt; delegate;</span><br><span class="line"></span><br><span class="line">/**把TabBarItem的Index发过来,在点击的时候再传回去*/</span><br><span class="line">@property(assign,nonatomic) NSInteger index;</span><br><span class="line"></span><br><span class="line">//	创建初始化方法</span><br><span class="line">- (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)topTitle andBottomTitle:(NSString *)bottmTitle;</span><br><span class="line"></span><br><span class="line">// 开始动画</span><br><span class="line">- (void)animation;</span><br><span class="line">//	隐藏自己</span><br><span class="line">- (void)hiddenPopoer;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="实现-属性"><a href="#实现-属性" class="headerlink" title="实现-属性"></a>实现-属性</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;TabbarItemPopoverView.h&quot;</span><br><span class="line">#import&lt;UIKit/UIKit.h&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">@interface TabbarItemPopoverView ()</span><br><span class="line"></span><br><span class="line">/*TopButton*/</span><br><span class="line">@property(strong,nonatomic) UIButton *topBTN;</span><br><span class="line">/*BottomBTN*/</span><br><span class="line">@property(strong,nonatomic) UIButton *bottomBTN;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="实现-初始化方法"><a href="#实现-初始化方法" class="headerlink" title="实现-初始化方法"></a>实现-初始化方法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">- (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)topTitle andBottomTitle:(NSString *)bottmTitle</span><br><span class="line">&#123;</span><br><span class="line">    self = [super initWithFrame:frame];</span><br><span class="line">    if (self) &#123;</span><br><span class="line">        self.topBTN = [UIButton buttonWithType:UIButtonTypeCustom];</span><br><span class="line">        [self.topBTN setTitle:topTitle forState:UIControlStateNormal];</span><br><span class="line">        self.topBTN.frame = CGRectMake(0, 0, frame.size.width, frame.size.height / 2);</span><br><span class="line">        [self.topBTN addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];</span><br><span class="line">        self.topBTN.tag = 2001;</span><br><span class="line">        self.topBTN.titleLabel.font = [UIFont systemFontOfSize:12];</span><br><span class="line">        [self addSubview:self.topBTN];</span><br><span class="line">        </span><br><span class="line">        self.bottomBTN = [UIButton buttonWithType:UIButtonTypeCustom];</span><br><span class="line">        [self.bottomBTN setTitle:bottmTitle forState:UIControlStateNormal];</span><br><span class="line">        self.bottomBTN.frame = CGRectMake(0, frame.size.height / 2, frame.size.width, frame.size.height / 2);</span><br><span class="line">        [self.bottomBTN addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];</span><br><span class="line">        self.bottomBTN.tag = 2002;</span><br><span class="line">        self.bottomBTN.titleLabel.font = [UIFont systemFontOfSize:12];</span><br><span class="line">        [self addSubview:self.bottomBTN];</span><br><span class="line">        self.backgroundColor = [UIColor clearColor];</span><br><span class="line">        </span><br><span class="line">        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, (frame.size.height - 3) / 2 - .25, frame.size.width, .5)];</span><br><span class="line">        line.backgroundColor = [UIColor lightGrayColor];</span><br><span class="line">        [self addSubview:line];</span><br><span class="line">    &#125;</span><br><span class="line">    return self;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-绘制视图"><a href="#实现-绘制视图" class="headerlink" title="实现-绘制视图"></a>实现-绘制视图</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">- (void)drawRect:(CGRect)rect&#123;</span><br><span class="line">    [super drawRect:rect];</span><br><span class="line">    </span><br><span class="line">    CGRect roundRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height - 8);</span><br><span class="line">    </span><br><span class="line">    CGPoint s = CGPointMake(rect.size.width / 2 - 7, roundRect.size.height + roundRect.origin.y);</span><br><span class="line">    CGPoint o = CGPointMake(rect.size.width / 2, rect.size.height + rect.origin.y);</span><br><span class="line">    CGPoint e = CGPointMake(rect.size.width / 2 + 7, roundRect.size.height + roundRect.origin.y);</span><br><span class="line">    </span><br><span class="line">    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:roundRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(6, 6)];</span><br><span class="line">    [path moveToPoint:s];</span><br><span class="line">    [path addLineToPoint:o];</span><br><span class="line">    [path addLineToPoint:e];</span><br><span class="line">    [path closePath];</span><br><span class="line">    [[UIColor lightGrayColor] setFill];</span><br><span class="line">    [path fill];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-动画"><a href="#实现-动画" class="headerlink" title="实现-动画"></a>实现-动画</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">- (void)animation&#123;</span><br><span class="line">    CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@&quot;transform&quot;];</span><br><span class="line">    NSValue *value1 = [NSValue valueWithCATransform3D:CATransform3DIdentity];</span><br><span class="line">    NSValue *value2 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.075, 1.075, 1)];</span><br><span class="line">    NSValue *value3 = [NSValue valueWithCATransform3D:CATransform3DIdentity];</span><br><span class="line">    scaleAnimation.values = @[value1,value2,value3];</span><br><span class="line">    scaleAnimation.keyTimes = @[@0,@0.7,@1];</span><br><span class="line">    </span><br><span class="line">    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@&quot;hidden&quot;];</span><br><span class="line">    baseAnimation.toValue = [NSNumber numberWithBool:false];</span><br><span class="line">    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];</span><br><span class="line">    groupAnimation.animations = @[scaleAnimation,baseAnimation];</span><br><span class="line">    groupAnimation.duration = 0.3;</span><br><span class="line">    groupAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];</span><br><span class="line">    groupAnimation.repeatCount = 1;</span><br><span class="line">    </span><br><span class="line">    self.layer.hidden = NO;</span><br><span class="line">    [self.layer addAnimation:groupAnimation forKey:@&quot;ani&quot;];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-hiddenPopoer"><a href="#实现-hiddenPopoer" class="headerlink" title="实现-hiddenPopoer"></a>实现-hiddenPopoer</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">- (void)hiddenPopoer&#123;</span><br><span class="line">    self.hidden = YES;</span><br><span class="line">    [self removeFromSuperview];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-点击事件"><a href="#实现-点击事件" class="headerlink" title="实现-点击事件"></a>实现-点击事件</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">- (void)selectButton:(UIButton*)btn&#123;</span><br><span class="line">    if ([self.delegate respondsToSelector:@selector(selecteItemIndex:buttonIndex:)]) &#123;</span><br><span class="line">        [self.delegate selecteItemIndex:self.index buttonIndex:btn.tag - 2000];</span><br><span class="line">    &#125;</span><br><span class="line">    [self hiddenPopoer];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="RootTabBarViewController中监听TabBarItem点击"><a href="#RootTabBarViewController中监听TabBarItem点击" class="headerlink" title="RootTabBarViewController中监听TabBarItem点击"></a>RootTabBarViewController中监听TabBarItem点击</h4><h5 id="RootTabBarViewController-接口"><a href="#RootTabBarViewController-接口" class="headerlink" title="RootTabBarViewController-接口"></a>RootTabBarViewController-接口</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@interface RootTabBarViewController : UITabBarController</span><br><span class="line"></span><br><span class="line">/**Index, 点击的下标使用通知传递到个个控制器,所以控制器初始化之前通知已经发送未能接收到通知消息,可以访问这个index来获取点击了那个按钮*/</span><br><span class="line">@property(assign,nonatomic,readonly) NSInteger index;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="实现-属性-1"><a href="#实现-属性-1" class="headerlink" title="实现-属性"></a>实现-属性</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">#import &quot;RootTabBarViewController.h&quot;</span><br><span class="line">#import &quot;TabbarItemPopoverView.h&quot;</span><br><span class="line"></span><br><span class="line">@interface RootTabBarViewController ()&lt;UITabBarControllerDelegate,TabbarItemPopoverViewDelegate&gt;</span><br><span class="line"></span><br><span class="line">/*PopoverView*/</span><br><span class="line">@property(strong,nonatomic) TabbarItemPopoverView *popover;</span><br><span class="line"></span><br><span class="line">/*tabBarItems 获取Frame时用到*/</span><br><span class="line">@property(copy,nonatomic) NSMutableArray *tabbarItems;</span><br><span class="line"></span><br><span class="line">/*titile数组二位*/</span><br><span class="line">@property(strong,nonatomic) NSMutableArray *titles;</span><br><span class="line"></span><br><span class="line">/**记录当前选中的Index*/</span><br><span class="line">@property(assign,nonatomic) NSInteger currentIndex;</span><br><span class="line"></span><br><span class="line">/**未来得及初始化的控制器可以访问这个属性获取index*/</span><br><span class="line">@property(assign,nonatomic) NSInteger index;</span><br><span class="line"></span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="实现-viewDidLoad"><a href="#实现-viewDidLoad" class="headerlink" title="实现-viewDidLoad"></a>实现-viewDidLoad</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">- (void)viewDidLoad &#123;</span><br><span class="line">    [super viewDidLoad];</span><br><span class="line">    </span><br><span class="line">    self.delegate = self;</span><br><span class="line">    self.currentIndex = 100;</span><br><span class="line">    </span><br><span class="line">    // 子控制器要隐藏Popover通知</span><br><span class="line">    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenPopover) name:@&quot;HiddenPopover&quot; object:nil];</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-代理"><a href="#实现-代理" class="headerlink" title="实现-代理"></a>实现-代理</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController&#123;</span><br><span class="line">    </span><br><span class="line">    //  第二次点击时先隐藏</span><br><span class="line">    if (self.popover) &#123;</span><br><span class="line">        [self.popover removeFromSuperview];</span><br><span class="line">        self.popover = nil;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    //  获取点击了第几个TabBarItem</span><br><span class="line">    NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];</span><br><span class="line">    </span><br><span class="line">    //  正在显示状态下点击同一个TabBarItem 就 Return</span><br><span class="line">    if (self.currentIndex == index) &#123;</span><br><span class="line">        self.currentIndex = 100;</span><br><span class="line">        return NO;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    //  tabbarItem是没有Frame的所以 转成View取出Frame</span><br><span class="line">    UIView *view = (UIView *)self.tabbarItems[index];</span><br><span class="line">    CGRect frame = view.frame;</span><br><span class="line">    </span><br><span class="line">    //  取出Popover上要显示的文字</span><br><span class="line">    NSArray &lt;NSString *&gt;*title = self.titles[index];</span><br><span class="line">    </span><br><span class="line">    //  先计算一下文字长度和占据宽度</span><br><span class="line">    NSString *maxTitle = title[0].length &gt; title[1].length ? title[0] : title[1];</span><br><span class="line"></span><br><span class="line">    CGFloat width = 0.0;</span><br><span class="line">    CGFloat x = 0.0;</span><br><span class="line">    //  最大文字宽高</span><br><span class="line">    CGSize titleSize = [self getTextSize:maxTitle];</span><br><span class="line">    </span><br><span class="line">    width = frame.size.width &gt; titleSize.width ? frame.size.width : titleSize.width;</span><br><span class="line">    x = frame.size.width &lt; titleSize.width ? frame.origin.x - (titleSize.width - frame.size.width) / 2 : frame.origin.x;</span><br><span class="line">    </span><br><span class="line">    //  创建Popover</span><br><span class="line">    self.popover = [[TabbarItemPopoverView alloc]initWithFrame:CGRectMake(x, [UIScreen mainScreen].bounds.size.height - tabBarController.tabBar.frame.size.height - 86, width, 85) andTitle:title[0] andBottomTitle:title[1]];</span><br><span class="line">    self.popover.index = index;</span><br><span class="line">    </span><br><span class="line">    self.popover.delegate = self;</span><br><span class="line">    //  添加</span><br><span class="line">    [self.view addSubview:self.popover];</span><br><span class="line">    </span><br><span class="line">    //  动画</span><br><span class="line">    [self.popover animation];</span><br><span class="line">    </span><br><span class="line">    //  记录当前Index</span><br><span class="line">    self.currentIndex = index;</span><br><span class="line">    </span><br><span class="line">    /*-----------调整TabBarItem状态------------*/</span><br><span class="line">    </span><br><span class="line">//    RootNavViewController *rootNav = (RootNavViewController *)viewController;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    return NO;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-代理方法"><a href="#实现-代理方法" class="headerlink" title="实现-代理方法"></a>实现-代理方法</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark - 点击了地级个Item的第几次按钮</span><br><span class="line">- (void)selecteItemIndex:(NSInteger)itemIndex buttonIndex:(NSInteger)btnIndex&#123;</span><br><span class="line">    self.currentIndex = 100;</span><br><span class="line">    self.selectedIndex = itemIndex;</span><br><span class="line">    self.index = btnIndex;</span><br><span class="line">    //  当前想到的办法就是通知,通知页面刷新数据....</span><br><span class="line">    [[NSNotificationCenter defaultCenter]postNotificationName:@&quot;CHANGECONTENT&quot; object:nil userInfo:@&#123;@&quot;itemIndex&quot; : @(itemIndex),@&quot;btnIndex&quot; : @(btnIndex)&#125;];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-隐藏Popover"><a href="#实现-隐藏Popover" class="headerlink" title="实现-隐藏Popover"></a>实现-隐藏Popover</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">#pragma mark - 隐藏Popover</span><br><span class="line">- (void)hiddenPopover&#123;</span><br><span class="line">    if (self.popover) &#123;</span><br><span class="line">        [self.popover hiddenPopoer];</span><br><span class="line">        self.currentIndex = 100;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="实现-懒加载"><a href="#实现-懒加载" class="headerlink" title="实现-懒加载"></a>实现-懒加载</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">- (CGSize )getTextSize:(NSString *)text&#123;</span><br><span class="line">    NSDictionary *dic = @&#123;NSFontAttributeName : [UIFont systemFontOfSize:12]&#125;;</span><br><span class="line">    //宽高限制，用于计算文本绘制时占据的矩形块。</span><br><span class="line">    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 0) options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context:nil].size;</span><br><span class="line">    size.width = size.width + 5;</span><br><span class="line">    return size;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (NSMutableArray *)tabbarItems&#123;</span><br><span class="line">    if (!_tabbarItems) &#123;</span><br><span class="line">        _tabbarItems = [self.tabBar.subviews mutableCopy];</span><br><span class="line">        [_tabbarItems removeObjectAtIndex:0];</span><br><span class="line">    &#125;</span><br><span class="line">    return _tabbarItems;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">- (NSMutableArray *)titles&#123;</span><br><span class="line">    if (!_titles) &#123;</span><br><span class="line">        _titles = [NSMutableArray array];</span><br><span class="line">        NSArray *elementOne = @[@&quot;提交纠纷&quot;,@&quot;在线沟通&quot;];</span><br><span class="line">        NSArray *elementTwo = @[@&quot;线上[律师/调解员]&quot;,@&quot;普法直播&quot;];</span><br><span class="line">        NSArray *elementThree = @[@&quot;[律师/调解员]登记&quot;,@&quot;个人中心&quot;];</span><br><span class="line">        [_titles addObject:elementOne];</span><br><span class="line">        [_titles addObject:elementTwo];</span><br><span class="line">        [_titles addObject:elementThree];</span><br><span class="line">    &#125;</span><br><span class="line">    return _titles;</span><br><span class="line">&#125;</span><br><span class="line">- (void)dealloc &#123;</span><br><span class="line">    [[NSNotificationCenter defaultCenter]removeObserver:self];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="https://gitee.com/Min_amdrl/tabbatItemClick" target="_blank" rel="noopener"><strong>Demo</strong></a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/13/多线程/" rel="next" title="多线程">
                <i class="fa fa-chevron-left"></i> 多线程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/10/25/颜色渐变/" rel="prev" title="颜色渐变">
                颜色渐变 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#遇到一个比较特殊的页面"><span class="nav-number">1.</span> <span class="nav-text">遇到一个比较特殊的页面</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#利用UITabBarControllerDelegate"><span class="nav-number">2.</span> <span class="nav-text">利用UITabBarControllerDelegate</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#首先创建RootController"><span class="nav-number">3.</span> <span class="nav-text">首先创建RootController</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#创建TabbarItemPopoverView-PopoverView"><span class="nav-number">4.</span> <span class="nav-text">创建TabbarItemPopoverView(PopoverView)</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#接口页"><span class="nav-number">4.1.</span> <span class="nav-text">接口页</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-属性"><span class="nav-number">4.2.</span> <span class="nav-text">实现-属性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-初始化方法"><span class="nav-number">4.3.</span> <span class="nav-text">实现-初始化方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-绘制视图"><span class="nav-number">4.4.</span> <span class="nav-text">实现-绘制视图</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-动画"><span class="nav-number">4.5.</span> <span class="nav-text">实现-动画</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-hiddenPopoer"><span class="nav-number">4.6.</span> <span class="nav-text">实现-hiddenPopoer</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-点击事件"><span class="nav-number">4.7.</span> <span class="nav-text">实现-点击事件</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#RootTabBarViewController中监听TabBarItem点击"><span class="nav-number">5.</span> <span class="nav-text">RootTabBarViewController中监听TabBarItem点击</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#RootTabBarViewController-接口"><span class="nav-number">5.1.</span> <span class="nav-text">RootTabBarViewController-接口</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-属性-1"><span class="nav-number">5.2.</span> <span class="nav-text">实现-属性</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-viewDidLoad"><span class="nav-number">5.3.</span> <span class="nav-text">实现-viewDidLoad</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-代理"><span class="nav-number">5.4.</span> <span class="nav-text">实现-代理</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-代理方法"><span class="nav-number">5.5.</span> <span class="nav-text">实现-代理方法</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-隐藏Popover"><span class="nav-number">5.6.</span> <span class="nav-text">实现-隐藏Popover</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#实现-懒加载"><span class="nav-number">5.7.</span> <span class="nav-text">实现-懒加载</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
